
var pageContext = new PageContext(settings||{});

pageContext.back = function(target){
	var $lis = $("#page-header>ul>li");
	var len = $lis.length ;
	$($lis.get(len-2)).find("a").click();
}
pageContext.addItemAfter = function(result,para){
	pageContext.back();
};
pageContext.updateItemAfter = function(result,para){
	pageContext.back();
};
pageContext.formInit = function(layero){
	
} 
pageContext.init = function(){//初始化
	pageContext.initEvent();
};
var selected = false;
function flag(){
	var $input = $("#applyeditForm input[name='p.nationalId']");
	var flag = $("#flag").val();
	if(flag==""){
		var typeahead = $input.typeahead({
			displayText:function(item){
				return typeof item !== 'undefined' && typeof item.nationalId != 'undefined' && item.nationalId || item;
			}
		,
		afterSelect:function(newval){
			if(!selected){
				selected = true;
			    var url = (window.global.ctx+'/auth/account/person?_json=0&id='+newval.id+"&userId="+$("#userId").val()); 
			   load(url,"page-content",false,"基本信息",function(){
			   });
			}
		},
		source: function (query, process) {
			var params = {"nationalId":query};
			var source = [];
			$.ajax({
				url:window.global.ctx+"/auth/account/listPerson?_style=list",
				type:'GET', //GET
				async:true,    //或false,是否异步
				data:params,
				timeout:5000,    //超时时间
				dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
				success:function(data,textStatus,jqXHR){
					source = (data)||[];
					process(source);
				} 
			});
			return source
		},
		autoSelect: true, 
		matcher: function (item) {
			selected = false;
			var it = this.displayText(item.nationalId)+"";
			var result = ~it.toLowerCase().indexOf(this.query.toLowerCase());
			return result;
		},
		});
		$input.on("change",function() {
			var current = $input.typeahead("getActive");
			console.log(current)
			if (current) {
				// Some item from your model is active!
				if (current.nationalId == $input.val()) {
					if(!selected){
						$input.typeahead('afterSelect',current)
					}
					// This means the exact match is found. Use toLowerCase() if you want case insensitive match.
				} else {
					// This means it is only a partial match, you can either add a new item
					// or take the active if you don't want new items
				}
			} else {
				// Nothing is active so it is a new value (or maybe empty value)
			}
		});
	}
}
jQuery(function($) {
	 $(document).find("input").each(function(){
		 $(this).attr("autocomplete","off");
	 });
	flag();
	$('#homeAddress').citys({
	dataUrl:window.global.ctx+"/static/fw/plugin/data_location/list.json?noPage=1"
	 ,code:$("#applyeditForm").find("select[name='a.provinceCode']").attr("value")
	 ,province:$("#applyeditForm").find("select[name='a.provinceCode']").attr("value")             
     ,city:$("#applyeditForm").find("select[name='a.cityCode']").attr("value")         
     ,area:$("#applyeditForm").find("select[name='a.districtCode']").attr("value")
	 ,provinceField:'a.provinceCode' //省份字段名
     ,cityField:'a.cityCode'         //城市字段名
     ,areaField:'a.districtCode'         //地区字段名
	 ,required:false
	,onChange:function(me,info){
		if(me!=null){
		   var p = $(me).attr("prefix");
		   if(p && p!=""){
			   var val = $(me).find("option:selected").val();
			   var text = "";
			   if(val!=""){
				   text = $(me).find("option:selected").text();
			   }
			   $('#applyeditForm input[name="'+p+'Name"]').val(text).change();
		   }
		   var $span =   $(me).css("width", "auto").hide().next("span");
		   var size = $(me).find("option").length;
		   if(size>0){
			   $span.show();
		   }else{
			   $span.hide();
		   }
		   $span.css("width", ($(me).outerWidth()+40)+"px"); 
		}
	},onReady:function(){
		$('#homeAddress').find('select[novalid="true"]').each(function(){
			$(this).hide().width("auto")
            .css("width", "auto").css("min-width", "80px").select2({
	            language: "zh-CN"}).removeClass("select2-hidden-accessible");
			
		})
	}});
	$('#currentAddress').citys({
		dataUrl:window.global.ctx+"/static/fw/plugin/data_location/list.json?noPage=1"
		 ,code:$("#applyeditForm").find("select[name='c.provinceCode']").attr("value")
		 ,province:$("#applyeditForm").find("select[name='c.provinceCode']").attr("value")             
	     ,city:$("#applyeditForm").find("select[name='c.cityCode']").attr("value")         
	     ,area:$("#applyeditForm").find("select[name='c.districtCode']").attr("value")
		 ,provinceField:'c.provinceCode' //省份字段名
	     ,cityField:'c.cityCode'         //城市字段名
	     ,areaField:'c.districtCode'         //地区字段名
	     ,required:false
		,onChange:function(me,info){
			if(me!=null){
			   var p = $(me).attr("prefix");
			   if(p && p!=""){
				   var val = $(me).find("option:selected").val();
				   var text = "";
				   if(val!=""){
					   text = $(me).find("option:selected").text();
				   }
				   $('#applyeditForm input[name="'+p+'Name"]').val(text).change();
			   }
			   var $span =   $(me).css("width", "auto").hide().next("span");
			   var size = $(me).find("option").length;
			   if(size>0){
				   $span.show();
			   }else{
				   $span.hide();
			   }
			   $span.css("width", ($(me).outerWidth()+40)+"px"); 
			}
		},onReady:function(){
			$('#currentAddress').find('select[novalid="true"]').each(function(){
				$(this).hide().width("auto")
	            .css("width", "auto").css("min-width", "80px").select2({
		            language: "zh-CN"}).removeClass("select2-hidden-accessible");
				
			})
		}});
	

	$(".home").on("change",function(){
		var map = new Map();
		$(".home").each(function(){
			map.put($(this).attr("index"),$(this).val());
		});
		var index = 1;
		var $input = $('#applyeditForm input[name="a.address"]');
		var str = "";
		while(index<=4){
			var v = map.get(index);
			if(v!=null && v!=undefined){
				str+=v;
			}
			index++;
		}
		$input.val(str);
	});
	$(".current").on("change",function(){
		var map = new Map();
		$(".current").each(function(){
			map.put($(this).attr("index"),$(this).val());
		});
		var index = 1;
		var $input = $('#applyeditForm input[name="c.address"]');
		var str = "";
		while(index<=4){
			var v = map.get(index);
			if(v!=null && v!=undefined){
				str+=v;
			}
			index++;
		}
		$input.val(str);
	});
	
	
	$("#applyeditForm .inputElem").mailAutoComplete({height:"100px"});
   pageContext.init({});
   $('#applyeditForm select[novalid!="true"]').on("change",function(){
	   var me = this;
	   var p = $(me).attr("prefix");
	   if(p && p!=""){
		   var val = $(me).find("option:selected").val();
		   var text = "";
		   if(val!=""){
			   text = $(me).find("option:selected").text();
		   }
		   $('#applyeditForm input[name="'+p+'Name"]').val(text).change();
	   }
   })
   var layero = $("#applyeditForm");
	layero.find('select[novalid!="true"]').each(function(){
		var novalid = $(this).attr("noconvert");
		if(novalid!="true"){
			layero.find(this).select2({
	            language: "zh-CN",width:"off"})
	            .next("span").removeClass("col-xs-10-select2").addClass("col-xs-10-select2");
		}
	  });
   $('#applyeditForm select').change();
	var form =  $("#"+pageContext.setting.formId);
	form.validationEngine('attach', {promptPosition : "topLeft", autoPositionUpdate : true});
	pageInit();
}); 
